document.addEventListener('DOMContentLoaded', function() {
    // 检查当前页面是否为登录或注册页面
    const currentPath = window.location.pathname;
    const isAuthPage = currentPath.includes('login.html') || currentPath.includes('register.html');
    
    // 只在非登录/注册页面创建移动端菜单按钮
    const header = document.querySelector('header');
    if (header && !document.querySelector('.mobile-menu-btn') && !isAuthPage) {
        // 创建菜单按钮
        const menuBtn = document.createElement('button');
        menuBtn.className = 'mobile-menu-btn';
        menuBtn.innerHTML = '☰';
        menuBtn.style.fontSize = '24px';
        menuBtn.style.color = '#fff';
        menuBtn.style.background = 'none';
        menuBtn.style.border = 'none';
        menuBtn.style.cursor = 'pointer';
        menuBtn.style.position = 'fixed';
        menuBtn.style.bottom = '20px';
        menuBtn.style.right = '20px';
        menuBtn.style.zIndex = '1000';
        menuBtn.style.width = '50px';
        menuBtn.style.height = '50px';
        menuBtn.style.borderRadius = '50%';
        menuBtn.style.backgroundColor = '#2ecc71';
        menuBtn.style.boxShadow = '0 2px 5px rgba(0,0,0,0.2)';
        document.body.appendChild(menuBtn);

        // 创建菜单弹窗
        const menuOverlay = document.createElement('div');
        menuOverlay.className = 'mobile-menu-overlay';
        menuOverlay.style.position = 'fixed';
        menuOverlay.style.top = '0';
        menuOverlay.style.left = '0';
        menuOverlay.style.width = '100%';
        menuOverlay.style.height = '100%';
        menuOverlay.style.backgroundColor = 'rgba(0,0,0,0.8)';
        menuOverlay.style.display = 'none';
        menuOverlay.style.opacity = '0';
        menuOverlay.style.transition = 'opacity 0.3s ease';
        menuOverlay.style.zIndex = '999';

        // 创建菜单容器
        const menuContainer = document.createElement('div');
        menuContainer.className = 'mobile-menu-container';
        menuContainer.style.position = 'absolute';
        menuContainer.style.top = '50%';
        menuContainer.style.left = '50%';
        menuContainer.style.transform = 'translate(-50%, -50%)';
        menuContainer.style.textAlign = 'center';

        // 获取原始导航菜单项
        const navItems = document.querySelectorAll('nav a');
        navItems.forEach(item => {
            // 检查是否为登录/注册按钮
            const isAuthButton = item.classList.contains('login-btn') || item.classList.contains('register-btn');
            // 如果已登录且是登录/注册按钮，则跳过不显示
            if (sessionStorage.getItem('auth_user') && isAuthButton) {
                return;
            }
            const menuItem = document.createElement('a');
            menuItem.href = item.href;
            menuItem.textContent = item.textContent;
            menuItem.style.display = 'block';
            menuItem.style.color = '#fff';
            menuItem.style.fontSize = '20px';
            menuItem.style.margin = '20px 0';
            menuItem.style.textDecoration = 'none';
            menuContainer.appendChild(menuItem);
        });

        menuOverlay.appendChild(menuContainer);
        document.body.appendChild(menuOverlay);

        // 添加点击事件
        menuBtn.addEventListener('click', function() {
            if (menuOverlay.style.display === 'none' || menuOverlay.style.display === '') {
                menuOverlay.style.display = 'block';
                setTimeout(() => {
                    menuOverlay.style.opacity = '1';
                }, 10);
            } else {
                menuOverlay.style.opacity = '0';
                setTimeout(() => {
                    menuOverlay.style.display = 'none';
                }, 300);
            }
        });

        menuOverlay.addEventListener('click', function(e) {
            if (e.target === menuOverlay) {
                menuOverlay.style.opacity = '0';
                setTimeout(() => {
                    menuOverlay.style.display = 'none';
                }, 300);
            }
        });
    }
}));